package kr.co.cherryt.view;

import kr.co.cherryt.activity.DetailPopupActivity;
import kr.co.cherryt.common.CC_Log;
import kr.co.cherryt.common.CherrytData;
import kr.co.cherryt.data.unit.UserInfo;
import kr.co.cherryt.layout.EventItemLayout;
import kr.co.cherryt.listView.BaseListView;
import kr.co.cherryt.listView.item.BaseItemLayout;
import kr.co.cherryt.parser.xml.XmlManager;
import kr.co.cherryt.parser.xml.XmlParserCore;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class EventListView extends BaseListView {
	
	public EventListView(Context context) {
		super(context);
		initControl(context);
	}

	public EventListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initControl(context);
	}
	
	private void initControl(Context context) {
		// 탭바에 리스트가 가려져 하단에 padding 주기
		setPadding(0, 0, 0, 20);
	}
	
	public void fillListViewInner(UserInfo userInfo, int mAdListPosition){
		
		String szUrl = CherrytData.getXmlUrl(getContext(), "ct_event_list.php");
		
		CC_Log.info("jin", "기업광고 리스트 : " + szUrl);
		
		setListItemUrl(szUrl);
	}
	
	@Override
	public View customGetView(int position, View convertView, ViewGroup parent, XmlManager parser) {
		
		Context context = getContext();
		BaseItemLayout layout = null;

		if(convertView == null) {
			layout = new EventItemLayout(context, m_childClickListener);
		}
		else {
			layout = (BaseItemLayout)convertView;
		}
		
		layout.fillServerData(context, parser, position);
		layout.m_nUniqueIdx = position;

		return layout;	
	}
	
	/**
	 * ListView에서 공통으로 사용될 클릭 리스너
	 */
	private OnClickListener m_childClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {

			int nIdx = BaseItemLayout.getLayoutId(v);
			
			if(nIdx > -1){
				onLayoutClick(v, nIdx, v.getId());
			}
		}
	};
	
	/**
	 * ListView와 childView의 클릭 리스너를 동시에 처리학 위한 함수
	 * @param view - childView 또는 ListView의 Layout
	 * @param position - 클릭된 List의 position
	 * @param id - chiildView인 경우 해당 view의 id, ListView인 경우 -1
	 */
	public void onLayoutClick(View view, int position, long id){ 
		Context context = getContext();
		
		CC_Log.info("jin", "리스트 클릭!!! : " + position);
		
		String szTitle = XmlParserCore.getXmlItemElementData(getXmlManager(), position, "title");
		String szSubTitle = XmlParserCore.getXmlItemElementData(getXmlManager(), position, "sub_title");
		String szImageSmall = XmlParserCore.getXmlItemElementData(getXmlManager(), position, "image_small");
		String szLink = XmlParserCore.getXmlItemElementData(getXmlManager(), position, "link");
		
		Intent intent = new Intent(context, DetailPopupActivity.class);
		intent.putExtra("title", szTitle);
		intent.putExtra("sub_con", szSubTitle);
		intent.putExtra("point", szImageSmall);
		intent.putExtra("link", szLink);
		((Activity) context).startActivityForResult(intent, CherrytData.GALLERY_DETAIL_RESULT_CODE);
	}
}
